﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PLock {
	partial class EditDlg : Form {
		public EditDlg(Entry entry, bool adding) {
			InitializeComponent();
			_entry = entry;
			_adding = adding;
		}

		private bool _adding;
		private Entry _entry;
		private bool _clearpasswords = false;

		private bool _loaded = false;
		private bool _labelchanged = false;
		private bool _userchanged = false;
		private bool _passchanged = false;
		private bool _snippetchanged = false;

		private void EditDlg_Load(object sender, EventArgs e) {
			try {
				textBoxLabel.Text = _entry.ReadLabel();
				textBoxUsername.Text = _entry.ReadUsername();
				if (_entry.HasPassword) {
					textBoxPassword.Text = "PLACEHOLDER";
					textBoxPasswordConfirm.Text = "PLACEHOLDER";
					_clearpasswords = true;
				}
				textBoxSnippet.Text = _entry.ReadSnippet();
				if (_adding)
					labelGuid.Text = "Unsaved";
				else
					labelGuid.Text = "GUID: " + _entry.Guid.ToString("D");
				UpdateEnabled();
				_loaded = true;
			} catch (Exception ex) {
				MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				this.Close();
			}
		}

		private void textBoxLabel_TextChanged(object sender, EventArgs e) {
			if (!_loaded) return;
			_labelchanged = true;
			UpdateEnabled();
		}

		private void textBoxUsername_TextChanged(object sender, EventArgs e) {
			if (!_loaded) return;
			_userchanged = true;
			UpdateEnabled();
		}

		private void textBoxPassword_TextChanged(object sender, EventArgs e) {
			if (!_loaded) return;
			_passchanged = true;
			UpdateEnabled();
		}

		private void textBoxPasswordConfirm_TextChanged(object sender, EventArgs e) {
			if (!_loaded) return;
			_passchanged = true;
			UpdateEnabled();
		}

		private void textBoxSnippet_TextChanged(object sender, EventArgs e) {
			if (!_loaded) return;
			_snippetchanged = true;
			UpdateEnabled();
		}

		private void UpdateEnabled() {
			buttonOK.Enabled = (
				textBoxLabel.Text.Trim() != "" &&
				!LabelCache.HasLabel(textBoxLabel.Text.Trim(), _entry.Guid) &&
				textBoxPassword.Text == textBoxPasswordConfirm.Text &&
					(textBoxUsername.Text.Trim() != "" ||
					(_clearpasswords || (PasswordDlg.IsValidPassword(textBoxPassword.Text) &&
										PasswordDlg.IsValidPassword(textBoxPasswordConfirm.Text))) ||
					textBoxSnippet.Text != "")
			);
		}

		private void buttonGenerate_Click(object sender, EventArgs e) {
			string pass = Settings.Instance.GeneratePassword();
			textBoxPassword.Text = pass;
			textBoxPasswordConfirm.Text = pass;
		}

		private void textBoxPassword_Enter(object sender, EventArgs e) {
			if (_clearpasswords) {
				textBoxPassword.Text = "";
				textBoxPasswordConfirm.Text = "";
				_clearpasswords = false;
			}
		}

		private void buttonOK_Click(object sender, EventArgs e) {
			if (_userchanged)
				_entry.WriteUsername(textBoxUsername.Text.Trim());
			if (_passchanged)
				_entry.WritePassword(textBoxPassword.Text.Trim());
			if (_snippetchanged)
				_entry.WriteSnippet(textBoxSnippet.Text);
			if (_labelchanged)
				_entry.WriteLabel(textBoxLabel.Text.Trim());
			LabelCache.InvalidateCache();
			this.DialogResult = DialogResult.OK;
		}
	}
}
